<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item :label="t('instanceReview.fields.awardsName')" prop="awardsName">
        <el-input
          v-model="queryParams.awardsName"
          :placeholder="t('common.inputTextPlaceholder') + t('instanceReview.fields.awardsName')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          {{ t('common.search') }}
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          {{ t('common.reset') }}
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iInstance-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <vxe-column
        :title="t('instanceReview.fields.awardsName')"
        field="awardsName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('instanceReview.fields.year')"
        field="year"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('instanceReview.fields.awardsType')"
        field="templateName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('instanceReview.fields.judgeStatus')"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      >
        <template #default="scope">
          {{ getJudgeStatus(scope.row.judgeStatus) }}
        </template>
      </vxe-column>
      <!-- <vxe-column -->
      <!--   :title="t('instanceReview.fields.awardsNum')" -->
      <!--   align="center" -->
      <!--   show-header-overflow -->
      <!--   show-overflow -->
      <!--   min-width="100" -->
      <!-- > -->
      <!--   <template #default="scope"> -->
      <!--     <p -->
      <!--       >{{ t('instanceReview.fields.total') }}: -->
      <!--       {{ (scope.row.masterCount ?? 0) + (scope.row.doctorCount ?? 0) }}</p -->
      <!--     > -->
      <!--     <p>{{ t('instanceReview.fields.masterTotal') }}: {{ scope.row.masterCount }}</p> -->
      <!--     <p>{{ t('instanceReview.fields.doctorTotal') }}: {{ scope.row.doctorCount }}</p> -->
      <!--   </template> -->
      <!-- </vxe-column> -->
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="180"
      >
        <template #default="scope">
          <el-button
            v-if="scope.row.hasJudge"
            link
            type="primary"
            :disabled="scope.row.judgeStatus === 1"
            @click="handleViewScene(scope.row)"
            :loading="checkLoading"
          >
            {{ t('instanceReview.index.judgeScene') }}
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import type { VxeTableInstance, VxeTablePropTypes, VxeToolbarInstance } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import { InstanceApi, TeacherInstanceVO } from '@/api/awards/instance'
import { JudgesSceneCheckVO, ReviewSceneApi } from '@/api/awards/reviewscene'

/** 评奖 列表 */
defineOptions({ name: 'Instance' })

const router = useRouter() // 路由对象
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()

const loading = ref(true) // 列表的加载中
const list = ref<TeacherInstanceVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  templateId: undefined,
  awardsName: undefined,
  year: undefined,
  noticeUrl: undefined,
  onlineApply: undefined,
  applyStartTime: [],
  applyEndTime: [],
  conditionJson: undefined,
  qualificationReview: undefined,
  differenceSelection: undefined,
  preliminaryEvaluation: undefined,
  awardsType: undefined,
  remark: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await InstanceApi.getJudgesInstancePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

// 计算评选状态 1=未开始 2=初评进行中 3=初评结束 4=终评进行中 5=终评结束
const getJudgeStatus = (status: number) => {
  switch (status) {
    case 1:
      return t('instanceReview.index.judgeStatus.notStarted')
    case 2:
      return t('instanceReview.index.judgeStatus.preliminaryEvaluation')
    case 3:
      return t('instanceReview.index.judgeStatus.preliminaryEvaluationEnd')
    case 4:
      return t('instanceReview.index.judgeStatus.finalEvaluation')
    case 5:
      return t('instanceReview.index.judgeStatus.finalEvaluationEnd')
    default:
      return ''
  }
}

const checkLoading = ref(false)
const handleViewScene = async (row: TeacherInstanceVO) => {
  if (!row.hasJudge || row.judgeStatus === 1) {
    return
  }
  // 先调接口校验是否能够进入
  checkLoading.value = true
  try {
    const result: JudgesSceneCheckVO = await ReviewSceneApi.checkJudgesSceneAvailable(row.id)
    if (result.isAvailable) {
      await router.push('/awards/review-judge-scene/' + row.id)
    } else {
      message.error(result.reason)
    }
  } finally {
    checkLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>

<style lang="scss" scoped>
:deep(.vxe-cell .el-button + .el-button) {
  margin-left: 0;
}
</style>
